﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcProject.Models;

namespace MvcProject.Controllers
{
    public class WorkTypeController : Controller
    {
        private BaseModels db = new BaseModels();
        //
        // GET: /WorkType/

        public ActionResult Index()
        {
            var contracts = db.FindAllWorkTypes();
            return View(contracts);
        }

        //
        // GET: /WorkType/Details/5

        public ActionResult Details(int id)
        {
            WorkType dinner = db.GetWorkType(id);
            return View(dinner);
        }

        //
        // GET: /WorkType/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /WorkType/Create

        [HttpPost]
        public ActionResult Create(WorkType dinner)
        {
            if (ModelState.IsValid)
                try
                {
                    UpdateModel(dinner);
                    dinner.CreateDate = System.DateTime.Now;
                    dinner.CreateUserName = "ywx";
                    db.Add(dinner);
                    db.Save();
                    return RedirectToAction("Details", new { id = dinner.WorkTypeId });
                }
                catch (Exception e)
                {
                    return View(dinner);
                }
            return View(dinner);
        }
        
        //
        // GET: /WorkType/Edit/5
 
        public ActionResult Edit(int id)
        {
            WorkType dinner = db.GetWorkType(id);
            return View(dinner);
        }

        //
        // POST: /WorkType/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            WorkType dinner = db.GetWorkType(id);
            UpdateModel(dinner);
            dinner.CreateDate = System.DateTime.Now;
            dinner.CreateUserName = "ywx";
            db.Save();
            return RedirectToAction("Details", new { id = dinner.WorkTypeId });
        }

        //
        // GET: /WorkType/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /WorkType/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            WorkType dinner = db.GetWorkType(id);
            if (dinner == null)
                return View("../ProjectInfo/NotFound");
            db.Delete(dinner);
            db.Save();
            return View("Index");
        }
    }
}
